package com.alibaba.citrus.cr.mn.order.center.facade.function.check;

import com.alibaba.citrus.cr.common.enums.ordercenter.*;
import com.alibaba.citrus.cr.common.exception.FacadeException;
import com.alibaba.citrus.cr.common.exception.FunctionException;
import com.alibaba.citrus.cr.common.util.DateUtils;
import com.alibaba.citrus.cr.mn.order.center.common.util.AssertUtils;
import com.alibaba.citrus.cr.mn.order.center.common.util.MoneyUtils;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.BaseDataServiceAdapter;
import com.alibaba.cz.base.tool.message.ErrorMessage;
import com.epoch.app.bcorder.platform_enhance.SaleOrderTag;
import com.epoch.app.mnordercenter.model.dto.DictionaryResponse;
import com.epoch.app.mnordercenter.orderbaserequest.OrderAtomUpdateRequest;
import com.epoch.app.mnordercenter.orderbaserequest.OrderAtomUpdateResponse;
import com.epoch.app.mnordercenter.orderbaserequest.OrderLineAtomCreateResponse;
import com.epoch.app.mnordercenter.orderbaserequest.OrderLineAtomUpdateRequest;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * @author zhangxiaorui
 * @ClassName SalesOrderCreateParamsCheck
 * @date 2023/05/11 16:35
 * @version: 1.0
 */
@Service
public class SalesOrderUpdateParamsCheck extends AbstractOrderParamsCheckService<OrderAtomUpdateRequest, OrderAtomUpdateResponse> {

    @Resource
    private BaseDataServiceAdapter baseDataServiceAdapter;
    @Override
    public void paramsEnumDictCheck(List<OrderAtomUpdateRequest> requestList, List<OrderAtomUpdateResponse> responseList) {
        //来源系统
        List<DictionaryResponse> fromSysResponseList = baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.FROM_SYS.getCode());
        Map<String, DictionaryResponse> fromSysMap = fromSysResponseList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));
        //业态
        List<DictionaryResponse> businessTypeResponseList = baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.BUSINESS_TYPE.getCode());
        Map<String, DictionaryResponse> businessTypeMap = businessTypeResponseList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));

        Iterator<OrderAtomUpdateRequest> iterator = requestList.iterator();
        while (iterator.hasNext()) {
            OrderAtomUpdateRequest t = iterator.next();
            OrderAtomUpdateResponse createResponse = new OrderAtomUpdateResponse();
            createResponse.setSourceOrderNo(t.getSalesOrderNo());
            try {
                if (fromSysMap.get(t.getFromSys()) == null) {
                    throw new FunctionException("OC-01-001-01-15-006", "来源系统编码");
                }
                if (businessTypeMap.get(t.getBusinessType()) == null) {
                    throw new FunctionException("OC-01-001-01-15-006", "业态编码");
                }
                if (StringUtils.isNotBlank(t.getDeliveryMethodCode())) {
                    SalesOrderDeliveryMethodEnum salesOrderDeliveryMethodEnum = SalesOrderDeliveryMethodEnum.of(t.getDeliveryMethodCode());
                    Optional.ofNullable(salesOrderDeliveryMethodEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "配送方式编码"));
                    t.setDeliveryMethodName(salesOrderDeliveryMethodEnum.getDesc());
                }
                if (StringUtils.isNotBlank(t.getOrderReasonCode())) {
                    Optional.ofNullable(SalesOrderReasonEnum.of(t.getOrderReasonCode())).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "订单原因编码"));
                }
            } catch (FunctionException e) {
                createResponse.setErrorCode(e.getErrorCode());
                createResponse.setMessage(e.getErrorMessage());
                responseList.add(createResponse);
                iterator.remove();
                continue;
            }
            if (CollectionUtils.isEmpty(t.getOrderDetails())) {
                continue;
            }
            for (OrderLineAtomUpdateRequest p : t.getOrderDetails()) {
                try {
                    if (StringUtils.isBlank(p.getLineNum())) {
                        if (StringUtils.isNotBlank(p.getCloseCode())) {
                            StockUnitEnum salesUnit = StockUnitEnum.of(p.getSalesUnitCode());
                            StockUnitEnum stockUnit = StockUnitEnum.of(p.getStockUnitCode());
                            Optional.ofNullable(salesUnit).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "销售单位编码"));
                            Optional.ofNullable(stockUnit).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "库存单位编码"));
                            // 覆盖用户传入数据
                            p.setSalesUnitName(salesUnit.getDesc());
                            p.setStockUnitName(stockUnit.getDesc());
                        }
                    }
                    if (StringUtils.isNotBlank(p.getLineNum())) {
                        if (StringUtils.isNotBlank(p.getCloseCode())) {
                            SalesOrderCloseEnum closeEnum = SalesOrderCloseEnum.of(p.getCloseCode());
                            Optional.ofNullable(closeEnum).orElseThrow(() -> new FunctionException("OC-01-001-01-15-006", "关闭原因编码"));
                            p.setRefuseReasonName(closeEnum.getDesc());
                        }
                    }
                } catch (FunctionException e) {
                    OrderLineAtomCreateResponse response = new OrderLineAtomCreateResponse();
                    response.setLineNum(p.getSourceLineNum());
                    response.setErrorCode(e.getErrorCode());
                    response.setMessage(e.getErrorMessage());
                    addErrorResponse(responseList, createResponse, response);
                    iterator.remove();
                    break;
                }
            }
        }
    }

    @Override
    public void paramsNotBlankCheck(List<OrderAtomUpdateRequest> requestList, List<OrderAtomUpdateResponse> responseList) {
        AssertUtils.isTrue(CollectionUtils.isEmpty(requestList), "OC-01-001-01-15-001", "单据列表");
        AssertUtils.isTrue(requestList.size() > 100, "OC-01-001-01-15-004", "单据列表", requestList.size(), "100");

        Iterator<OrderAtomUpdateRequest> iterator = requestList.iterator();
        while (iterator.hasNext()) {
            OrderAtomUpdateRequest request = iterator.next();
            OrderAtomUpdateResponse createResponse = new OrderAtomUpdateResponse();
            createResponse.setSalesOrderNo(request.getSalesOrderNo());

            try {
                AssertUtils.isTrue(StringUtils.isBlank(request.getFromSys()), "OC-01-001-01-15-001", "来源系统编码");
                AssertUtils.isTrue(StringUtils.isBlank(request.getBusinessType()), "OC-01-001-01-15-001", "业态编码");
                AssertUtils.isTrue(StringUtils.isBlank(request.getBusinessUnitCode()), "OC-01-001-01-15-001", "业务单元编码");
                AssertUtils.isTrue(StringUtils.isBlank(request.getSalesOrderNo()), "OC-01-001-01-15-001", "销售单单号");
                AssertUtils.isTrue(CollectionUtils.isEmpty(request.getOrderDetails()), "OC-01-001-01-15-001", "单据明细");
                AssertUtils.isTrue(StringUtils.isBlank(request.getModifierCode()), "OC-01-001-01-15-001", "修改人工号");
                AssertUtils.isTrue(StringUtils.isBlank(request.getModifierName()), "OC-01-001-01-15-001", "修改人名称");
                if (CollectionUtils.isNotEmpty(request.getOrderDetails())) {
                    AssertUtils.isTrue(request.getOrderDetails().size() > 100, "OC-01-001-01-15-004", "单据明细", request.getOrderDetails().size(), "100");
                }
                // 来源单行号不能重复
                List<String> sameSourceLineNums = request.getOrderDetails().stream()
                        .map(OrderLineAtomUpdateRequest::getSourceLineNum)
                        .filter(StringUtils::isNotBlank)
                        .collect(Collectors.toMap(p -> p, p -> 1, Integer::sum))
                        .entrySet().stream().filter(entry -> entry.getValue() > 1)
                        .map(Map.Entry::getKey).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(sameSourceLineNums)) {
                    throw new FunctionException("OC-01-001-01-15-002", "来源单行号", StringUtils.join(sameSourceLineNums, ","));
                }
                // 明细行号不能重复
                List<String> sameSourceLines = request.getOrderDetails().stream()
                        .map(OrderLineAtomUpdateRequest::getLineNum)
                        .filter(StringUtils::isNotBlank)
                        .collect(Collectors.toMap(p -> p, p -> 1, Integer::sum))
                        .entrySet().stream().filter(entry -> entry.getValue() > 1)
                        .map(Map.Entry::getKey).collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(sameSourceLines)) {
                    throw new FunctionException("OC-01-001-01-15-002", "行号", StringUtils.join(sameSourceLines, ","));
                }
            } catch (FunctionException e) {
                createResponse.setErrorCode(e.getErrorCode());
                createResponse.setMessage(e.getErrorMessage());
                responseList.add(createResponse);
                iterator.remove();
                continue;
            }
            if (CollectionUtils.isEmpty(request.getOrderDetails())) {
                continue;
            }

            for (OrderLineAtomUpdateRequest detail : request.getOrderDetails()) {
                try {
                    AssertUtils.isTrue(StringUtils.isBlank(detail.getSourceLineNum()), "OC-01-001-01-15-001", "来源单行号");
                    if (StringUtils.isNotBlank(detail.getLineNum())) {
                        if (StringUtils.isNotBlank(detail.getShippingWarehouseCode())) {
                            continue;
                        }
                        if (StringUtils.isBlank(detail.getDeliveryFactoryCode()) && StringUtils.isBlank(detail.getStockCode())) {
                            throw new FunctionException("OC-01-001-01-15-010", "逻辑仓库编码、交货工厂编码、发货库存地点编码");
                        }
                        AssertUtils.isTrue(StringUtils.isBlank(detail.getDeliveryFactoryCode()), "OC-01-001-01-15-001", "交货工厂编码");
                        AssertUtils.isTrue(StringUtils.isBlank(detail.getStockCode()), "OC-01-001-01-15-001", "库存地点编码");
                    } else {
                        // AssertUtils.isTrue(StringUtils.isBlank(detail.getSourceLineNum()), "OC-01-001-01-15-001", "来源单行号");
                        AssertUtils.isTrue(StringUtils.isBlank(detail.getProductQuantity()), "OC-01-001-01-15-001", "销售数量");
                        AssertUtils.isTrue(StringUtils.isBlank(detail.getStockConversionNum()), "OC-01-001-01-15-001", "库存转换数量");
                        AssertUtils.isTrue(StringUtils.isBlank(detail.getUnitConversionRelationship()), "OC-01-001-01-15-001", "单位转换关系");
                        AssertUtils.isTrue(StringUtils.isBlank(detail.getUnitPrice()), "OC-01-001-01-15-001", "常规单价");
                        AssertUtils.isTrue(StringUtils.isBlank(detail.getTotalAmount()), "OC-01-001-01-15-001", "常规总金额");
                        AssertUtils.isTrue(StringUtils.isBlank(detail.getProductCode()), "OC-01-001-01-15-001", "物料编码");
                        AssertUtils.isTrue(StringUtils.isBlank(detail.getProductName()), "OC-01-001-01-15-001", "物料名称");
                        AssertUtils.checkRequired(Pair.of(detail.getSalesUnitCode(), detail.getSalesUnitName()), "OC-01-001-01-15-001", "销售单位编码、名称(都)");
                        AssertUtils.checkRequired(Pair.of(detail.getStockUnitCode(), detail.getStockUnitName()), "OC-01-001-01-15-001", "库存单位编码、名称(都)");
                        if (StringUtils.isBlank(detail.getShippingWarehouseCode())) {
                            if (StringUtils.isBlank(detail.getDeliveryFactoryCode()) && StringUtils.isBlank(detail.getStockCode())) {
                                throw new FunctionException("OC-01-001-01-15-010", "逻辑仓库编码、交货工厂编码、库存地点编码");
                            }
                            AssertUtils.isTrue(StringUtils.isBlank(detail.getDeliveryFactoryCode()), "OC-01-001-01-15-001", "交货工厂编码");
                            AssertUtils.isTrue(StringUtils.isBlank(detail.getStockCode()), "OC-01-001-01-15-001", "库存地点编码");
                        } else {
                            AssertUtils.isTrue(StringUtils.isBlank(detail.getShippingWarehouseName()), "OC-01-001-01-15-001", "逻辑仓库名称");
                        }

                        if (!OrderCategoryEnum.GROUP.getCode().equals(request.getOrderCategoryCode())) {
                            // 退货订单 手工单价校验区别于销售订单
                            //1.不做现金订单类型校验  2.手工总金额如果为空，要单价*销售数量算出来
                            if (!SalesOrderTypeEnum.CASH_ORDER.equals(SalesOrderTypeEnum.of(request.getOrderType()))) {
                                continue;
                            }
                            AssertUtils.isTrue(StringUtils.isBlank(detail.getManualPrice()), "OC-01-001-01-15-001", "(销售公司现金销售订单)手工单价");
                            if (StringUtils.isBlank(detail.getManualTotalAmount())) {
                                BigDecimal totalAmount;
                                try {
                                    BigDecimal manualPrice = new BigDecimal(detail.getManualPrice());
                                    BigDecimal productQuantity = new BigDecimal(detail.getProductQuantity());
                                    totalAmount = manualPrice.multiply(productQuantity);
                                } catch (Exception e) {
                                    throw new FacadeException(ErrorMessage.code("OC-01-001-01-15-006", "手工金额及销售数量"));
                                }
                                detail.setManualTotalAmount(totalAmount.toPlainString());
                            }
                            AssertUtils.isTrue(StringUtils.isBlank(detail.getManualTotalAmount()), "OC-01-001-01-15-001", "(销售公司现金销售订单)手工总金额");
                        }
                    }
                } catch (FunctionException e) {
                    OrderLineAtomCreateResponse response = new OrderLineAtomCreateResponse();
                    response.setLineNum(detail.getLineNum());
                    response.setErrorCode(e.getErrorCode());
                    response.setMessage(e.getErrorMessage());
                    addErrorResponse(responseList, createResponse, response);
                    iterator.remove();
                    break;
                }
            }
        }
    }

    @Override
    public void paramsLengthCheck(List<OrderAtomUpdateRequest> requestList, List<OrderAtomUpdateResponse> responseList) {
        Iterator<OrderAtomUpdateRequest> iterator = requestList.iterator();
        while (iterator.hasNext()) {
            OrderAtomUpdateRequest request = iterator.next();

            boolean isGroup = OrderCategoryEnum.GROUP.getCode().equals(request.getOrderCategoryCode());

            OrderAtomUpdateResponse createResponse = new OrderAtomUpdateResponse();
            createResponse.setSourceOrderNo(request.getSalesOrderNo());
            try {
                AssertUtils.isNumber(request.getBusinessType(), true, "OC-01-001-01-15-006", "业态编码");
                // 必填校验
                AssertUtils.lengthCheck(request.getFromSys(), 50, true, "OC-01-001-01-15-004", "来源系统编码");
                AssertUtils.lengthCheck(request.getBusinessType(), 10, true, "OC-01-001-01-15-004", "业态编码");
                AssertUtils.lengthCheck(request.getBusinessUnitCode(), 50, true, "OC-01-001-01-15-004", "业务单元编码");
                AssertUtils.lengthCheck(request.getSalesOrderNo(), 50, true, "OC-01-001-01-15-004", "退货单单号");
                AssertUtils.lengthCheck(request.getShipToPartyCode(), 50, false, "OC-01-001-01-15-004", "客户送达方编码");
                AssertUtils.lengthCheck(request.getShipToPartyName(), 50, false, "OC-01-001-01-15-004", "客户送达方名称");
                AssertUtils.lengthCheck(request.getNote(), 255, false, "OC-01-001-01-15-004", "备注");
                AssertUtils.lengthCheck(request.getBizFeatures(), 1024, false, "OC-01-001-01-15-004", "扩展字段");
                AssertUtils.lengthCheck(request.getModifierCode(), 50,true,"OC-01-001-01-15-001", "修改人工号");
                AssertUtils.lengthCheck(request.getModifierName(), 50,false,"OC-01-001-01-15-001", "修改人名称");
                AssertUtils.lengthCheck(request.getSalesChannelCode(), 50, false, "OC-01-001-01-15-004", "销售渠道编码");
                AssertUtils.lengthCheck(request.getSalesChannelName(), 50, false, "OC-01-001-01-15-004", "销售渠道名称");
                AssertUtils.lengthCheck(request.getSalesDepartmentCode(), 50, false, "OC-01-001-01-15-004", "销售部门编码");
                AssertUtils.lengthCheck(request.getSalesDepartmentName(), 50, false, "OC-01-001-01-15-004", "销售部门名称");
                AssertUtils.lengthCheck(request.getReceiveWarehouseCode(), 50, false, "OC-01-001-01-15-004", "收货仓编码");
                AssertUtils.lengthCheck(request.getReceiveFactoryCode(), 50, false, "OC-01-001-01-15-004", "收货工厂编码");
                AssertUtils.lengthCheck(request.getReceiveStockCode(), 50, false, "OC-01-001-01-15-004", "收货库存地点编码");
                // 日期格式校验
                if (StringUtils.isNotBlank(request.getExpectedDeliveryDate())) {
                    AssertUtils.lengthCheck(request.getExpectedDeliveryDate(), 10, false, "OC-01-001-01-15-004", "期望交货日期");
                    AssertUtils.isTrue(!DateUtils.isDateFormat(request.getExpectedDeliveryDate()), "OC-00-001-01-15-001", "期望交货日期");
//                    AssertUtils.isTrue(DateUtils.formatDate(new Date()).compareTo(request.getExpectedDeliveryDate()) > 0, "OC-01-001-01-16-021");
                }
                AssertUtils.lengthCheck(request.getDeliveryMethodCode(), 50, false, "OC-01-001-01-15-004", "配送方式编码");
                AssertUtils.lengthCheck(request.getOrderReasonCode(), 50, false, "OC-01-001-01-15-004", "订单原因编码");
                AssertUtils.lengthCheck(request.getOrderReasonName(), 50, false, "OC-01-001-01-15-004", "订单原因名称");
            } catch (FunctionException e) {
                createResponse.setErrorCode(e.getErrorCode());
                createResponse.setMessage(e.getErrorMessage());
                responseList.add(createResponse);
                iterator.remove();
                continue;
            }
            if (CollectionUtils.isEmpty(request.getOrderDetails())) {
                continue;
            }

            for (OrderLineAtomUpdateRequest detail : request.getOrderDetails()) {
                if (StringUtils.isBlank(detail.getLineNum())) {
                    //新增行校验
                    try {
                        AssertUtils.isWholeNumber(detail.getProductQuantity(), true, "OC-00-001-01-15-001", "销售数量");
                        AssertUtils.isNumber(detail.getManualTotalAmount(), false, "OC-00-001-01-15-001", "手工总金额");
                        AssertUtils.isNumber(detail.getManualPrice(), false, "OC-00-001-01-15-001", "手工单价");
                        AssertUtils.isNumber(detail.getStockConversionNum(), true, "OC-00-001-01-15-001", "转换数量");
                        AssertUtils.isTrue(new BigDecimal("2147483647").compareTo(new BigDecimal(detail.getProductQuantity())) < 0, "OC-01-001-01-15-009", "销售数量", "最大2147483647");
                        AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getProductQuantity())) >= 0, "OC-01-001-01-15-009", "销售数量", "不允许为零或负数");
                        AssertUtils.isTrue(new BigDecimal("2147483647").compareTo(new BigDecimal(detail.getStockConversionNum())) < 0, "OC-01-001-01-15-009", "转换数量", "最大2147483647");
                        AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getStockConversionNum())) >= 0, "OC-01-001-01-15-009", "转换数量", "不允许为零或负数");
                        if (StringUtils.isNotBlank(detail.getManualTotalAmount())) {
                            AssertUtils.isTrue(new BigDecimal("2147483647").compareTo(new BigDecimal(detail.getManualTotalAmount())) < 0, "OC-01-001-01-15-009", "手工总金额", "最大2147483647");
                            AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getManualTotalAmount())) >= 0, "OC-01-001-01-15-009", "手工总金额", "不允许为零或负数");
                        }
                        if (StringUtils.isNotBlank(detail.getManualPrice())) {
                            AssertUtils.isTrue(new BigDecimal("2147483647").compareTo(new BigDecimal(detail.getManualPrice())) < 0, "OC-01-001-01-15-009", "手工单价", "最大2147483647");
                            AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getManualPrice())) >= 0, "OC-01-001-01-15-009", "手工单价", "不允许为零或负数");
                        }
                        AssertUtils.isTrue(new BigDecimal("2147483647").compareTo(new BigDecimal(detail.getUnitPrice())) < 0, "OC-01-001-01-15-009", "常规单价", "最大2147483647");
                        AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getUnitPrice())) >= 0, "OC-01-001-01-15-006", "常规单价", "不允许为零或负数");
                        AssertUtils.lengthCheck(detail.getSourceLineNum(), 10, true, "OC-01-001-01-15-004", "来源单行号");
                        AssertUtils.lengthCheck(detail.getProductCode(), 50, true, "OC-01-001-01-15-004", "物料编码");
                        AssertUtils.lengthCheck(detail.getProductName(), 50, true, "OC-01-001-01-15-004", "物料名称");
                        AssertUtils.lengthCheck(detail.getProductQuantity(), 10, true, "OC-01-001-01-15-004", "销售数量");
                        AssertUtils.lengthCheck(detail.getSalesUnitCode(), 50, true, "OC-01-001-01-15-004", "销售单位编码");
                        AssertUtils.lengthCheck(detail.getSalesUnitName(), 50, true, "OC-01-001-01-15-004", "销售单位名称");
                        AssertUtils.lengthCheck(detail.getStockUnitCode(), 50, true, "OC-01-001-01-15-004", "库存单位编码");
                        AssertUtils.lengthCheck(detail.getStockUnitName(), 50, true, "OC-01-001-01-15-004", "库存单位名称");
                        AssertUtils.lengthCheck(detail.getUnitPrice(), 10, false, "OC-01-001-01-15-004", "常规单价");
                        AssertUtils.lengthCheck(detail.getUnitConversionRelationship(), 50, true, "OC-01-001-01-15-004", "单位转换关系");
                        AssertUtils.lengthCheck(detail.getInnerProductCode(), 50, false, "OC-01-001-01-15-004", "产品编码");
                        AssertUtils.lengthCheck(detail.getInnerProductName(), 50, false, "OC-01-001-01-15-004", "产品名称");
                        AssertUtils.lengthCheck(detail.getProductBarCode(), 50, false, "OC-01-001-01-15-004", "产品条码");
                        AssertUtils.lengthCheck(detail.getNote(), 255, false, "OC-01-001-01-15-004", "订单行备注");
                        AssertUtils.lengthCheck(detail.getActivityTotalAmount(), 10, false, "OC-01-001-01-15-004", "活动总金额");
                        AssertUtils.lengthCheck(detail.getBuyItemNo(), 50, false, "OC-01-001-01-15-004", "原品行号");
                        AssertUtils.lengthCheck(detail.getManualTotalAmount(), 10, false, "OC-01-001-01-15-004", "手工总金额");
                        AssertUtils.lengthCheck(detail.getManualPrice(), 10, false, "OC-01-001-01-15-004", "手工单价");

                        AssertUtils.lengthCheck(detail.getShippingWarehouseCode(), 50, false, "OC-01-001-01-15-004", "逻辑仓库编码");
                        AssertUtils.lengthCheck(detail.getShippingWarehouseName(), 50, false, "OC-01-001-01-15-004", "逻辑仓库名称");
                        AssertUtils.lengthCheck(detail.getDeliveryFactoryCode(), 50, false, "OC-01-001-01-15-004", "交货工厂编码");
                        AssertUtils.lengthCheck(detail.getDeliveryFactoryName(), 50, false, "OC-01-001-01-15-004", "交货工厂名称");
                        AssertUtils.lengthCheck(detail.getStockCode(), 50, false, "OC-01-001-01-15-004", "库存地点编码");
                        AssertUtils.lengthCheck(detail.getStockName(), 50, false, "OC-01-001-01-15-004", "库存地点名称");
                        AssertUtils.lengthCheck(detail.getProductionDate(), 10, false, "OC-01-001-01-15-004", "生产日期");
                        AssertUtils.lengthCheck(detail.getBizFeatures(), 1024, false, "OC-01-001-01-15-004", "扩展字段");

                        // 金额参数 不允许为零或负数
                        AssertUtils.moneyCheck(detail.getUnitPrice(), true, "OC-01-001-01-15-005", "常规单价");
                        AssertUtils.moneyCheck(detail.getManualTotalAmount(), false, "OC-01-001-01-15-005", "手工总金额");
                        AssertUtils.moneyCheck(detail.getManualPrice(), false, "OC-01-001-01-15-005", "手工单价");

                        if (!isGroup) {
                            AssertUtils.isNumber(detail.getTotalAmount(), true, "OC-00-001-01-15-001", "常规总金额");
                            AssertUtils.isTrue(new BigDecimal("2147483647").compareTo(new BigDecimal(detail.getTotalAmount())) < 0, "OC-01-001-01-15-009", "常规总金额", "最大2147483647");
                            AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getTotalAmount())) >= 0, "OC-01-001-01-15-009", "常规总金额", "不允许为零或负数");
                            AssertUtils.lengthCheck(detail.getTotalAmount(), 10, false, "OC-01-001-01-15-004", "常规总金额");
                            AssertUtils.moneyCheck(detail.getTotalAmount(), false, "OC-01-001-01-15-005", "常规总金额");

                            AssertUtils.lengthCheck(detail.getStockConversionNum(), 10, true, "OC-01-001-01-15-004", "库存转换数量");
                        }
                    } catch (FunctionException e) {
                        OrderLineAtomCreateResponse response = new OrderLineAtomCreateResponse();
                        response.setLineNum(detail.getSourceLineNum());
                        response.setErrorCode(e.getErrorCode());
                        response.setMessage(e.getErrorMessage());
                        addErrorResponse(responseList, createResponse, response);
                        iterator.remove();
                        break;
                    }
                    // 金额 NYC(元) 1.01 转 数据库(分) 101
                    resetMoneyForNYC(detail);
                }

                if (StringUtils.isNotBlank(detail.getLineNum())) {
                    //修改行校验
                    try {
                        if (StringUtils.isNotBlank(detail.getProductQuantity())) {
                            AssertUtils.lengthCheck(detail.getProductQuantity(), 10, true, "OC-01-001-01-15-004", "销售数量");
                            AssertUtils.isWholeNumber(detail.getProductQuantity(), true, "OC-00-001-01-15-001", "销售数量");
                        }
                        if (StringUtils.isNotBlank(detail.getSourceLineNum())) {
                            AssertUtils.lengthCheck(detail.getSourceLineNum(), 10, true, "OC-01-001-01-15-004", "来源单行号");
                        }
                        if (StringUtils.isNotBlank(detail.getProductCode())) {
                            AssertUtils.lengthCheck(detail.getProductCode(), 50, true, "OC-01-001-01-15-004", "物料编码");
                        }
                        if (StringUtils.isNotBlank(detail.getProductName())) {
                            AssertUtils.lengthCheck(detail.getProductName(), 50, false, "OC-01-001-01-15-004", "物料名称");
                        }
                        if (StringUtils.isNotBlank(detail.getProductQuantity())) {
                            AssertUtils.lengthCheck(detail.getProductQuantity(), 10, true, "OC-01-001-01-15-004", "销售数量");
                            AssertUtils.isTrue(new BigDecimal("2147483647").compareTo(new BigDecimal(detail.getProductQuantity())) < 0, "OC-01-001-01-15-006", "销售数量", "最大2147483647");
                            AssertUtils.isTrue(BigDecimal.ZERO.compareTo(new BigDecimal(detail.getProductQuantity())) >= 0, "OC-01-001-01-15-006", "销售数量", "不允许为零或负数");
                        }
                        if (StringUtils.isNotBlank(detail.getSalesUnitCode())) {
                            AssertUtils.lengthCheck(detail.getSalesUnitCode(), 50, true, "OC-01-001-01-15-004", "销售单位编码");
                        }
                        if (StringUtils.isNotBlank(detail.getSalesUnitName())) {
                            AssertUtils.lengthCheck(detail.getSalesUnitName(), 50, false, "OC-01-001-01-15-004", "销售单位名称");
                        }
                        if (StringUtils.isNotBlank(detail.getStockUnitCode())) {
                            AssertUtils.lengthCheck(detail.getStockUnitCode(), 50, true, "OC-01-001-01-15-004", "库存单位编码");
                        }
                        if (StringUtils.isNotBlank(detail.getStockUnitName())) {
                            AssertUtils.lengthCheck(detail.getStockUnitName(), 50, false, "OC-01-001-01-15-004", "库存单位名称");
                        }
                        if (StringUtils.isNotBlank(detail.getUnitPrice())) {
                            AssertUtils.lengthCheck(detail.getUnitPrice(), 10, false, "OC-01-001-01-15-004", "常规单价");
                        }
                        if (StringUtils.isNotBlank(detail.getUnitConversionRelationship())) {
                            AssertUtils.lengthCheck(detail.getUnitConversionRelationship(), 10, false, "OC-01-001-01-15-004", "单位转换关系");
                        }
                        if (StringUtils.isNotBlank(detail.getInnerProductCode())) {
                            AssertUtils.lengthCheck(detail.getInnerProductCode(), 50, false, "OC-01-001-01-15-004", "产品编码");
                        }
                        if (StringUtils.isNotBlank(detail.getInnerProductName())) {
                            AssertUtils.lengthCheck(detail.getInnerProductName(), 50, false, "OC-01-001-01-15-004", "产品名称");
                        }
                        if (StringUtils.isNotBlank(detail.getProductBarCode())) {
                            AssertUtils.lengthCheck(detail.getProductBarCode(), 50, false, "OC-01-001-01-15-004", "产品条码");
                        }
                        if (StringUtils.isNotBlank(detail.getNote())) {
                            AssertUtils.lengthCheck(detail.getNote(), 255, false, "OC-01-001-01-15-004", "订单行备注");
                        }
                        if (StringUtils.isNotBlank(detail.getActivityTotalAmount())) {
                            AssertUtils.lengthCheck(detail.getActivityTotalAmount(), 10, false, "OC-01-001-01-15-004", "活动总金额");
                        }
                        if (StringUtils.isNotBlank(detail.getBuyItemNo())) {
                            AssertUtils.lengthCheck(detail.getBuyItemNo(), 50, false, "OC-01-001-01-15-004", "原品行号");
                        }
                        if (StringUtils.isNotBlank(detail.getManualTotalAmount())) {
                            AssertUtils.lengthCheck(detail.getManualTotalAmount(), 10, false, "OC-01-001-01-15-004", "手工总金额");
                        }
                        if (StringUtils.isNotBlank(detail.getManualPrice())) {
                            AssertUtils.lengthCheck(detail.getManualPrice(), 10, false, "OC-01-001-01-15-004", "手工单价");
                        }
                        if (StringUtils.isNotBlank(detail.getShippingWarehouseCode())) {
                            AssertUtils.lengthCheck(detail.getShippingWarehouseCode(), 50, false, "OC-01-001-01-15-004", "逻辑仓库编码");
                        }
                        if (StringUtils.isNotBlank(detail.getShippingWarehouseName())) {
                            AssertUtils.lengthCheck(detail.getShippingWarehouseName(), 50, false, "OC-01-001-01-15-004", "逻辑仓库名称");
                        }
                        if (StringUtils.isNotBlank(detail.getDeliveryFactoryCode())) {
                            AssertUtils.lengthCheck(detail.getDeliveryFactoryCode(), 50, false, "OC-01-001-01-15-004", "交货工厂编码");
                        }
                        if (StringUtils.isNotBlank(detail.getDeliveryFactoryName())) {
                            AssertUtils.lengthCheck(detail.getDeliveryFactoryName(), 50, false, "OC-01-001-01-15-004", "交货工厂名称");
                        }
                        if (StringUtils.isNotBlank(detail.getStockCode())) {
                            AssertUtils.lengthCheck(detail.getStockCode(), 50, false, "OC-01-001-01-15-004", "库存地点编码");
                        }
                        if (StringUtils.isNotBlank(detail.getStockName())) {
                            AssertUtils.lengthCheck(detail.getStockName(), 50, false, "OC-01-001-01-15-004", "库存地点名称");
                        }
                        if (StringUtils.isNotBlank(detail.getProductionDate())) {
                            AssertUtils.lengthCheck(detail.getProductionDate(), 10, false, "OC-01-001-01-15-004", "生产日期");
                        }
                        // 金额参数 不允许为零或负数
                        AssertUtils.moneyCheck(detail.getUnitPrice(), false, "OC-01-001-01-15-005", "常规单价");
                        AssertUtils.moneyCheck(detail.getTotalAmount(), false, "OC-01-001-01-15-005", "常规总金额");
                        AssertUtils.moneyCheck(detail.getManualPrice(), false, "OC-01-001-01-15-005", "手工单价");
                        AssertUtils.moneyCheck(detail.getManualTotalAmount(), false, "OC-01-001-01-15-005", "手工总金额");

                        if (!isGroup) {
                            if (StringUtils.isNotBlank(detail.getTotalAmount())) {
                                AssertUtils.lengthCheck(detail.getTotalAmount(), 10, false, "OC-01-001-01-15-004", "常规总金额");
                            }
                            if (StringUtils.isNotBlank(detail.getStockConversionNum())) {
                                AssertUtils.lengthCheck(detail.getStockConversionNum(), 10, true, "OC-01-001-01-15-004", "库存转换数量");
                            }
                        }
                    } catch (FunctionException e) {
                        OrderLineAtomCreateResponse response = new OrderLineAtomCreateResponse();
                        response.setLineNum(detail.getSourceLineNum());
                        response.setErrorCode(e.getErrorCode());
                        response.setMessage(e.getErrorMessage());
                        addErrorResponse(responseList, createResponse, response);
                        iterator.remove();
                        break;
                    }
                }
            }
        }
    }

    void addErrorResponse(List<OrderAtomUpdateResponse> responseList, OrderAtomUpdateResponse parent, OrderLineAtomCreateResponse chiled) {
        List<OrderLineAtomCreateResponse> errorChildList = new ArrayList<>();
        errorChildList.add(chiled);
        parent.setOrderDetails(errorChildList);
        responseList.add(parent);
    }

    /**
     * 金额转换 元转分
     */
    static void resetMoneyForNYC(OrderLineAtomUpdateRequest request) {
        request.setUnitPrice(MoneyUtils.moneyStrYuan2Fen(request.getUnitPrice()));
        request.setTotalAmount(MoneyUtils.moneyStrYuan2Fen(request.getTotalAmount()));
        request.setManualPrice(MoneyUtils.moneyStrYuan2Fen(request.getManualPrice()));
        request.setManualTotalAmount(MoneyUtils.moneyStrYuan2Fen(request.getManualTotalAmount()));
        request.setActivityPrice(MoneyUtils.moneyStrYuan2Fen(request.getActivityPrice()));
        request.setActivityTotalAmount(MoneyUtils.moneyStrYuan2Fen(request.getActivityTotalAmount()));
    }
}
